package com.cction.comm;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Utils {

    /**
     * String 转 Int
     *
     * @param value
     * @return
     */
    public static Integer stringToInt(String value) {
        if (value == null) {
            return 0;
        }
        try {
            return Integer.parseInt(value);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * String 转 Double
     *
     * @param value
     * @return
     */
    public static double stringToDouble(String value) {
        if (value == null) {
            return 0d;
        }
        try {
            return Double.parseDouble(value);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0d;
    }

    /**
     * 日期转化
     *
     * @param milliseconds
     * @param format
     * @return
     */
    public static String formatDate(long milliseconds, String format) {
        String resultD = "";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            Date d = new Date(milliseconds);
            resultD = sdf.format(d);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultD;
    }

    /**
     * 保留小数点后两位数字
     */
    public static double keepPointDouble(double value) {
        BigDecimal bg = new BigDecimal(value);
        return bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * 保留小数点后两位数字
     */
    public static String keepPointString(double value) {
        BigDecimal bg = new BigDecimal(value);
        return bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + "";
    }
}
